#ifndef _SCUDB_BTREE_H_
#define _SCUDB_BTREE_H_

#include "sd_global.h"
#include "sd_blist.h"

#define SD_BTREE_MAX_NUM_CHILDNODE 32


typedef struct sd_BTreeNode
{
	bool isUsed;
	bool isLeaf;
	int nodeIndex;
	int parentNodeIndex;
	int depth;
	sd_Key key; /* The smallest key in all records within it */
	sd_Blist records;	
	int childNodeIndexes[SD_BTREE_MAX_NUM_CHILDNODE];
	int numChildNodes;
}sd_BTreeNode;

typedef struct sd_BTree
{
	size_t size;
	int rootNodeIndex;
	int depth;
	int maxNumChildren;
	int maxNumNodes;
	int maxDepth;
	size_t recordsNodeBufSize;
	sd_KeyCompare key_cmp;
	sd_BTreeNode nodes[1];
}sd_BTree;


sd_Ret sd_BTreeCreate(sd_BTree** tree, sd_KeyCompare key_cmp, int maxNumNodes, int maxChild, 
		int maxDepth, size_t recordsNodeBufSize);
sd_Ret sd_BTreeFree(sd_BTree* tree);
sd_Ret sd_BTreeInsert(sd_BTree* tree, const sd_Record* record);
sd_Ret sd_BTreeSearch(sd_BTree* tree, const sd_Key* key, sd_Record* record);
sd_Ret sd_BTreeDelete(sd_BTree* tree, const sd_Record* record);
sd_Ret sd_BTreePrint(sd_BTree* tree, FILE* fp);


#endif

